﻿#include "form_gpio.h"
#include "com_rs232.h"
#include "ui_form_gpio.h"

#include <QPushButton>
#include <com_manager.h>
#include <gpio_info.h>

#pragma execution_character_set("utf-8")


Form_GPIO::Form_GPIO(  QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::Form_GPIO)
{
    ui->setupUi(this);

    gpio_id =0;

    ui->comboBox_port->addItems(com_manager::instance()->comList);

    if (com_manager::instance()->comList.count()>0)
    com_name = com_manager::instance()->comList[0];

    ui->stackedWidget->setCurrentIndex(0);

    ui->radio_mode_input->setChecked(true);

    setWindowTitle(QString("GPIO-")+com_name);

    GPIO * _gpio = GpioSetting::gpio[com_name][gpio_id];

    ui->label_gpio->setText("寄存器值："+_gpio->toString());

    rs232com* rs232 = com_manager::instance()->get_com(com_name);
    if (rs232)
    {
        if (rs232->myCom&&rs232->myCom->isOpen())
        {
            ui->pushButton_open_com->setText("关闭");
            ui->pushButton_read->setEnabled(true);
        }
        else//串口没有打开
        {
            ui->pushButton_open_com->setText("打开");
            ui->pushButton_read->setEnabled(false);
        }
    }


//    connect(ui->page_input,  &Widget_GPIO_Input::changePage,[=]{
//        GPIO * _gpio = GpioSetting::gpio[com_name][gpio_id];

//        ui->page_input->  _gpio =*  _gpio;
//        ui->page_input->  _gpio.mode_input = 1;
//        ui->page_output->  _gpio =*  _gpio;
//        ui->page_output->  _gpio.mode_input = 0;

//        ui->page_input->updateChart();
//        ui->page_output->updateChart();

//        ui->radio_mode_input->setChecked(_gpio->mode_input);
//        ui->radio_mode_output->setChecked(!_gpio->mode_input);
//        if (_gpio->mode_input )
//            ui->stackedWidget->setCurrentIndex(0);
//        else
//            ui->stackedWidget->setCurrentIndex(1);
//    });
//    connect(ui->page_output,  &Widget_GPIO_Output::changePage,[=]{
//        GPIO * _gpio = GpioSetting::gpio[com_name][gpio_id];

//        ui->radio_mode_input->setChecked(_gpio->mode_input);

//        ui->radio_mode_output->setChecked(!_gpio->mode_input);
//        ui->page_input->  _gpio =*  _gpio;
//        ui->page_input->  _gpio.mode_input = 1;
//        ui->page_output->  _gpio =*  _gpio;
//        ui->page_output->  _gpio.mode_input = 0;

//        ui->page_input->updateChart();
//        ui->page_output->updateChart();

//        if (_gpio->mode_input )
//            ui->stackedWidget->setCurrentIndex(0);
//        else
//            ui->stackedWidget->setCurrentIndex(1);
//    });
}

Form_GPIO::~Form_GPIO()
{
    delete ui;
}



void Form_GPIO::on_radio_mode_input_clicked(bool checked)
{
    if ( checked)
    {
        ui->stackedWidget->setCurrentIndex(0);
        Widget_GPIO_Input * page = ( Widget_GPIO_Input *)ui->stackedWidget->currentWidget();
        page->updateChart() ;
    }
    else {
        ui->stackedWidget->setCurrentIndex(1);

    }
}


void Form_GPIO::on_radio_mode_output_clicked(bool checked)
{
    if ( checked)
    {
        ui->stackedWidget->setCurrentIndex(1);
        Widget_GPIO_Output * page = ( Widget_GPIO_Output *)ui->stackedWidget->currentWidget();
        page->updateChart() ;
    }
    else {
        ui->stackedWidget->setCurrentIndex(0);
    }
}


void Form_GPIO::on_comboBox_gpio_currentIndexChanged(int index)
{
    gpio_id =index;

    GPIO * _gpio = GpioSetting::gpio[com_name][gpio_id];

    ui->label_gpio->setText("寄存器值："+_gpio->toString());

    ui->page_input->initChart(com_name, index );
    ui->page_output-> initChart(com_name, index );
}


void Form_GPIO::on_comboBox_port_currentTextChanged(const QString &arg1)
{
    com_name = arg1;
    int index = ui->comboBox_gpio->currentIndex();

    rs232com* rs232 = com_manager::instance()->get_com(com_name);
    if (rs232)
    {
        if (   rs232 ->myCom && rs232 ->myCom ->isOpen())
        {
            ui->pushButton_open_com->setText("关闭");
            ui->pushButton_read->setEnabled(true);
         }
        else
        {
            ui->pushButton_open_com->setText("打开");
            ui->pushButton_read->setEnabled(false);
         }
    }

    ui->page_input ->initChart(com_name, index );
    ui->page_output->initChart(com_name, index );

    setWindowTitle(QString("GPIO-")+com_name);
}


void Form_GPIO::on_pushButton_open_com_clicked()
{
    rs232com* rs232 = com_manager::instance()->get_com(com_name);
    if (rs232)
    {
        if (rs232->myCom&&rs232->myCom->isOpen())
        {
            rs232->close_port();
            ui->pushButton_open_com->setText("打开");
            ui->pushButton_read->setEnabled(false);
        }
        else//串口没有打开
        {
          bool opened=   rs232->openPort(com_name, BaudRateType::BAUD115200, DataBitsType::DATA_8, ParityType::PAR_NONE, StopBitsType::STOP_1);
          if (opened)
              ui->pushButton_open_com->setText("关闭");

          ui->pushButton_read->setEnabled(true);
        }
    }

    ui-> page_input->updateChart();
   ui-> page_output->updateChart();



}

void Form_GPIO::on_pushButton_read_clicked()
{
    rs232com* rs232 = com_manager::instance()->get_com(com_name);
    if (rs232)
    {
        rs232->query_gpio(gpio_id);
        GPIO * _gpio = GpioSetting::gpio[com_name][gpio_id];

        ui->label_gpio->setText("寄存器值："+_gpio->toString());

        ui->radio_mode_input->setChecked(_gpio->mode_input);

        ui->radio_mode_output->setChecked(!_gpio->mode_input);
        ui->page_input->  _gpio =*  _gpio;
        ui->page_input->  _gpio.mode_input = 1;
        ui->page_output->  _gpio =*  _gpio;
        ui->page_output->  _gpio.mode_input = 0;

        ui->page_input->updateChart();
        ui->page_output->updateChart();

        if (_gpio->mode_input )
            ui->stackedWidget->setCurrentIndex(0);
        else
            ui->stackedWidget->setCurrentIndex(1);

        GpioSetting::save_gpio_setting();
    }
    else
    {
        mHelper::ShowMessageBoxError("读取失败");
    }
}
